<?php
class Articles extends MY_Controller{
	var $beforeFilter = array(
			'initAdmin' => array(
				'only' => array('admin','add','remove','edit','save'),
			),
			'_init' => array(
				'only' => array('index','view'),
			),
		);

	function __construct()
	{
		parent::MY_Controller();
		$this->load->helper(array('url','system','form'));
		$this->load->model('Article');
	}

	function view($id=null){
		$this->load->model('ArticleCat');
		$article = $this->Article->findByPk($id);
		if(!empty($article)){
			if(!empty($article['link'])){
				redirect($article['link'], 'refresh');
				exit();
			}
			$category = $this->ArticleCat->findByPk($article['cat_id']);
			$this->assign['article'] = $article;
			$this->assign['category'] = $category;
			if(!empty($article['tpls'])) $this->template = $article['tpls'];
		}
		$this->display();
	}
	
	function index($cid=0)
	{
		//显示状态为1
		$_REQUEST['is_open'] = 1;

		$this->load->model('ArticleCat');
		$category = $this->ArticleCat->findByPk($cid);
		$this->assign['category'] = $category;
		$this->assign['rs'] = $this->_lists($cid);
		if(!empty($category['tpls'])) $this->template = $category['tpls'];
		$this->display();
	}

	function block($cid=0){
		//显示状态为1
		$_REQUEST['is_open'] = 1;

		$this->assign['rs'] = $this->_lists($cid);
		$this->display();
	}

	function _lists($cid = 0){
		if($cid>0){
			$this->load->model('ArticleCat');
			$rs = $this->ArticleCat->doList();
			$cates = cat_options($cid,$rs);
			$cids = array();
			foreach($cates as $cate){
				$cids[] = $cate['id'];
			}
			if(empty($cids)) return null;
			$this->Article->db->where_in('cat_id', $cids);
		}
		$this->Article->db->where_in('article_type', array(0,1));
		$like = $this->_searchLike($this->Article);
		$where = $this->_searchWhere($this->Article);
		$map = array();
		if(!empty($like)) $map['like'] = $like;
		if(!empty($where)) $map['where'] = $where;
		$join = array('table'=>'articlecats','on'=>'articlecats.id=articles.cat_id');
		$rs = $this->_list($this->Article,$map,$join,'articles.*,articlecats.name',true,'update_on',false);
		
		return $rs;
	}

	function admin(){
		$cid = !empty($_REQUEST['cat_id']) ? intval($_REQUEST['cat_id']) : 0;
		unset($_REQUEST['cat_id']);
		$this->assign['rs'] = $this->_lists($cid);
		$_REQUEST['cat_id'] = $cid;
		$this->template = "articles_admin";
		$this->display();
	}
	

	function save(){
		$redirect = $this->getRedirect('article_admin');
		$rules['title']	= "trim|required|min_length[3]|max_length[100]";
		$rules['content']	= "trim|required|min_length[5]";
		if ($this->validation($rules,'details') == FALSE)
		{
			//验证错误信息
			$this->showMessage($this->validation->error_string); 
		}
		
		//上传文件
		$dir = '';
		if(!empty($this->configure['upload_path_format'])) 
			$dir = date($this->configure['upload_path_format']);
		$config['upload_path'] = $this->configure['upload_photo_path']."/".$dir;
		if(!is_dir($config['upload_path'])) do_mkdirs($config['upload_path']);
		$config['allowed_types'] = $this->configure['allowed_photo_types'];
		$config['encrypt_name'] = TRUE;
		$this->load->library('upload', $config);
		$data = $_POST;
		$isUploaded = false;
		if ( $this->upload->do_upload('userfile')){
			$upload = $this->upload->data();
			$data['file_url'] = $dir."/".$upload['raw_name'].$upload['file_ext'];
			$width =  intval($this->configure['articles_upload_thumb_width']);
			$height =  intval($this->configure['articles_upload_thumb_height']);
			if(!empty($width)&&!empty($height)){
				$_path = $this->configure['upload_photo_path'].'/thumb/'.$dir.'/';
				if(!is_dir($_path))do_mkdirs($_path);
				$iconfig['source_image'] = $config['upload_path'].'/'.$upload['raw_name'].$upload['file_ext'];
				$iconfig['new_image'] = $_path.$upload['raw_name'].$upload['file_ext'];
				$iconfig['width'] = $width;
				$iconfig['height'] = $height;
				$iconfig['maintain_ratio'] = false;
				$this->load->library('image_lib', $iconfig); 
				$this->image_lib->resize();
			}
			$isUploaded = true;
		}
		//如果修改使用的是绝对路径的文件，将原来上传的文件删除
		if(strstr($data['file_url'],'http://'))$isUploaded = true;

		if($isUploaded && !empty($_POST['ofile_url']) && !strstr($data['ofile_url'],'http://') ){
			if(file_exists($this->configure['upload_photo_path']."/".$_POST['ofile_url'])){
				@unlink($this->configure['upload_photo_path']."/".$_POST['ofile_url']);
				@unlink($this->configure['upload_photo_path']."/thumb/".$_POST['ofile_url']);
			}
		}

		if($this->Article->save($data)){
			$this->showMessage('mes_success',$redirect);
		}else{
			$this->showMessage($this->Article->_error); 
		}
	}

	function add(){
		$this->template = "articles_save";
		$this->display();
	}

	function edit($id){
		$res = $this->Article->findByPk($id);
		$this->assign['result'] = $res;
		$this->template = "articles_save";
		$this->display();
	}
	
	function remove(){
		$redirect = $this->getRedirect('admin');
		$id = empty($id) ? $_REQUEST['id'] : $id;
		$rs = $this->Article->findByPks($id,array('file_url !='=>''));
		$path = $this->configure['upload_photo_path'];
		if(!empty($rs)){
			foreach($rs as $article){
				if(strstr($article['file_url'],'http://')!==false || !file_exists($path.'/'.$article['file_url']))continue;
				@unlink($path."/".$article['file_url']);
				@unlink($path."/thumb/".$article['file_url']);
			}
		}
		$this->Article->removeByPks($id);
		$this->showMessage('mes_success',$redirect);
	}
}